﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Rabbit.Logic;
using RabbitShop.Code;
using Raven.Imports.Newtonsoft.Json;

namespace RabbitShop.Action
{
    public partial class GroupCartItemAdd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string uid = string.Empty;
            if (Request.Cookies[Constant.CookieName] != null)
            {
                uid = Request.Cookies[Constant.CookieName].Values[Constant.CookieUid];
                if (string.IsNullOrEmpty(uid)) Response.Redirect("~/UI/UserLogon.aspx");
            }
            else
            {
                Response.Redirect("~/UI/UserLogon.aspx");
            }


            Stream postData = Request.InputStream;
            StreamReader sRead = new StreamReader(postData);
            string postContent = sRead.ReadToEnd();
            sRead.Close();

            if (!string.IsNullOrEmpty(postContent))
            {
                try
                {
                    ShopCartItem item = JsonConvert.DeserializeObject<ShopCartItem>(postContent);
                    Product product = RabbitUse.GetProductById(Db.Store, item.ProductId);

                    Customer customer = RabbitUse.GetCustomerById(Db.Store, uid);
                    if (customer.ShopList == null)
                    {
                        List<ShopCartItem> shopCartItems = new List<ShopCartItem>() {item};
                        ShopItem shopItem = new ShopItem()
                                                          {
                                                              ShopId = product.ShopId,
                                                              CartItemList = shopCartItems
                                                          };
                        customer.ShopList = new List<ShopItem>();
                        customer.ShopList.Add(shopItem);
                    }
                    else
                    {
                        ShopItem targetShopItem = customer.ShopList.FirstOrDefault(x => x.ShopId == product.ShopId);
                        if (targetShopItem == null)
                        {
                            List<ShopCartItem> shopCartItems = new List<ShopCartItem>() { item };
                            ShopItem shopItem = new ShopItem()
                            {
                                ShopId = product.ShopId,
                                CartItemList = shopCartItems
                            };
                            customer.ShopList.Add(shopItem);
                        }
                        else
                        {
                            ShopCartItem shopCartItem = targetShopItem.CartItemList.FirstOrDefault(x => x.ProductId == item.ProductId);
                            if (shopCartItem == null)
                            {
                                targetShopItem.CartItemList.Add(item);
                            }
                            else
                            {
                                shopCartItem.Quantity += item.Quantity;
                            }
                        }
                    }
                    
                    //Shop shop = RabbitUse.GetShopByShopId(Db.Store, product.ShopId);


                    //bool isExist = false;
                    //for (int i = 0; i < customer.Cart.Count; i++)
                    //{
                    //    if (customer.Cart[i].ProductId == item.ProductId)
                    //    {
                    //        customer.Cart[i].Quantity += item.Quantity;
                    //        isExist = true;
                    //        break;
                    //    }
                    //}
                    //if (!isExist)
                    //{
                    //    customer.Cart.Add(item);
                    //}
                    RabbitUse.AddOrUpdateCustomer(Db.Store, customer);

                    Logic.UpdateCookieCartNo(this, customer.ShopList.Count.ToString());

                    Response.Write("true");
                }
                catch (Exception ex)
                {
                    Response.Write(ex.Message);
                }

            }
            else
            {
                Response.Write("false");
            }
        }
    }
}